﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace GaiNo.Core.Services
{
    public class Log : ILog
    {
        private static readonly object _lock = new object();
        private static ILog _log = null;
        public static ILog Current
        {
            get
            {
                if (_log == null)
                {
                    lock (_lock)
                    {
                        if (_log == null)
                        {
                            _log = new Log();
                        }
                    }
                }
                return _log;
            }
        }

        #region ILog 成员

        public void ApplicationError(string thread, string message)
        {
            WriteLog(thread, message, Priority.HIGH, System.Diagnostics.TraceEventType.Error);
        }

        public void SystemError(string thread, string message)
        {
            WriteLog(thread, message, Priority.HIGHEST, System.Diagnostics.TraceEventType.Error);
        }

        public void ApplicationWran(string thread, string message)
        {
            WriteLog(thread, message, Priority.NORMAL, System.Diagnostics.TraceEventType.Warning);
        }

        public void SystemWran(string thread, string message)
        {
            WriteLog(thread, message, Priority.NORMAL, System.Diagnostics.TraceEventType.Warning);
        }
        public void Debug(string thread, string message)
        {
            WriteLog(thread, message, Priority.HIGHEST, System.Diagnostics.TraceEventType.Information);
        }

        #endregion

        private void WriteLog(string thread, string message, Priority priority, System.Diagnostics.TraceEventType type)
        {
            LogEntry log = new LogEntry();
            log.Severity = type;
            log.Title = thread;
            log.Message = message;
            log.Priority = (int)priority;
            log.Categories.Add(Category.General);
            Logger.Write(log);
        }

        #region ILog 成员


        public void Information(string thread, string message)
        {
            WriteLog(thread, message, Priority.LOWEST, System.Diagnostics.TraceEventType.Information);
        }

        #endregion
    }
}
